﻿using System;
using RenderHeads.Media.AVProVideo;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Assets.Scripts.Commom
{

    public enum MediaState
    {
        Pause,//暂停
        Play,//播放
        Stop,//停止
    }

    public class VideoSeekBar:MonoBehaviour,IDragHandler,IPointerUpHandler,IPointerDownHandler
    {

        private MediaPlayer mediaPlayer;//播放器
        private Slider videoSlider;//进度滑动条
        private Text movieTimeText;//视频的时间
        private Text totalTimeText;//视频的总时间

        private int totalTime;//总的时间
        private float setSliderValue;
        private MediaState mediaState;

        public bool isPlay;//是否播放视频   
        public bool IsDrag;//是否是拖动滑动条的状态

        void Awake()
        {
            videoSlider=transform.GetComponent<Slider>();
            movieTimeText = transform.parent.Find("Time/MovieTimeText").GetComponent<Text>();
            totalTimeText = transform.parent.Find("Time/TotalTimeText").GetComponent<Text>();
        }


        /// <summary>
        /// Video事件
        /// </summary>
        /// <param name="mp"></param>
        /// <param name="et"></param>
        /// <param name="errorCode"></param>
        public void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
        {
            switch (et)
            {
                case MediaPlayerEvent.EventType.ReadyToPlay:
                    break;
                case MediaPlayerEvent.EventType.Started:
                    OnPlay();
                    break;
                case MediaPlayerEvent.EventType.FirstFrameReady:
                    break;
                case MediaPlayerEvent.EventType.FinishedPlaying:
                    OnEnd();
                    break;
            }
            Debug.Log("视频的事件：" + et.ToString());
            Debug.Log("错误码：" + errorCode);// 没有视频
        }

        void Update()
        {
            if (!isPlay)
            {
                return;
            }
            //更新滑动条
            if (mediaPlayer && mediaPlayer.Info != null && mediaPlayer.Info.GetDurationMs() > 0f)
            {
                float time = mediaPlayer.Control.GetCurrentTimeMs();
                float d = time / mediaPlayer.Info.GetDurationMs();
                setSliderValue = d;
                videoSlider.value = d;
            }
            InitTotalTime();
            movieTimeText.text = UpdateMovieTime();
        }

        /// <summary>
        /// 提供一个外部调用的接口
        /// </summary>
        /// <param name="mediaPlayer"></param>
        public void Init(MediaPlayer mediaPlayer)
        {
            this.mediaPlayer = mediaPlayer;
            mediaPlayer.Events.AddListener(OnVideoEvent);
            if (mediaPlayer.Control == null) return;
            if (mediaPlayer.Control.IsPlaying())
            {
                OnPlay();
            }
            else
            {
                OnPause();
            }
        }

        private void OnEnd()
        {
            isPlay = false;
            videoSlider.value = 0;
        }

        /// <summary>
        /// 视频开始播放
        /// </summary>
        public void OnPlay()
        {
            isPlay = true;
        }

        //视频暂停
        public void OnPause()
        {
            isPlay = false;
        }


        //开始拖拽
        public void OnDrag(PointerEventData eventData)
        {
            movieTimeText.text = UpdateMovieTime();
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (mediaPlayer.Control.IsPlaying()) mediaState = MediaState.Play;
            if (mediaPlayer.Control.IsPaused()) mediaState = MediaState.Pause;
            isPlay = false;
            IsDrag = true;
            mediaPlayer.Control.Pause();
        }

        //拖拽结束的事件
        public void OnPointerUp(PointerEventData eventData)
        {
            IsDrag = false;
            if (mediaPlayer != null && mediaPlayer.Control != null && !string.IsNullOrEmpty(mediaPlayer.m_VideoPath))
            {
                OnVideoSeekSlider();
                switch (mediaState)
                {
                    case MediaState.Play:
                        isPlay = true;
                        if (mediaPlayer)
                        {
                            mediaPlayer.Control.Play();
                        }
                        break;
                    case MediaState.Pause:
                        break;
                }
            }
            else
            {
                videoSlider.value = 0;
            }
            OnVideoSeekSlider();
        }

        private void OnVideoSeekSlider()
        {
            if (mediaPlayer && videoSlider && Math.Abs(videoSlider.value - setSliderValue) > 0.01f)
            {
                mediaPlayer.Control.Seek(videoSlider.value * mediaPlayer.Info.GetDurationMs());
            }
        }

        /// <summary>
        /// 初始化总的视频时间
        /// </summary>
        private void InitTotalTime()
        {
            if (mediaPlayer == null) return;
            if (totalTime == (int)mediaPlayer.Info.GetDurationMs() / 1000)
            {
                return;
            }
            Debug.Log("初始化总的时间");
            string s_min = "";
            string s_seconds = "";
            int min = 0, seconds = 0;
            totalTime = (int)mediaPlayer.Info.GetDurationMs() / 1000;
            min = totalTime / 60;
            seconds = totalTime - min * 60;
            if (min < 10)
            {
                s_min = 0 + min.ToString();
            }
            else
            {
                s_min = min.ToString();
            }
            if (seconds < 10)
            {
                s_seconds = 0 + seconds.ToString();
            }
            else
            {
                s_seconds = seconds.ToString();
            }
            totalTimeText.text = "/"+s_min + ":" + s_seconds;
        }

        /// <summary>
        /// 更新视频的时间
        /// </summary>
        /// <returns></returns>
        private string UpdateMovieTime()
        {
            string s_min = "";
            string s_seconds = "";
            int movieTime = (int)(mediaPlayer.Info.GetDurationMs() * videoSlider.value) / 1000;
            int min = movieTime / 60;
            int seconds = movieTime - min * 60;
            if (min < 10)
            {
                s_min = 0 + min.ToString();
            }
            else
            {
                s_min = min.ToString();
            }
            if (seconds < 10)
            {
                s_seconds = 0 + seconds.ToString();
            }
            else
            {
                s_seconds = seconds.ToString();
            }
            return s_min + ":" + s_seconds;
        }
    }
}